package com.icehand.json.benchmark;

import com.icehand.echarts.model.EchartsOption;
import com.icehand.json.model.User;
import com.icehand.json.utils.*;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.results.RunResult;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
 * 反序列化
 * @author zhoujunmei
 * @date 2019-08-02 11:02
 * @remark
 */
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
public class JsonDeserializeBenchmark {
    @Param({"1000","10000","100000"})
    private int count;

    private String jsonStr;

    public static void main(String[] args) throws Exception {
        Options option = new OptionsBuilder()
                .include(JsonDeserializeBenchmark.class.getSimpleName())
                .forks(1)
                .warmupIterations(0)
                .build();
        Collection<RunResult> results = new Runner(option).run();
        EchartsOption echartsOption = ResultExporter.exportEchartsResult("JSON反序列化性能", results, "count", "秒");
        System.out.println(OkHttpUtil.post("http://localhost:9075/api/v1/data",echartsOption));
    }

    @Benchmark
    public void jackson() {
        for (int i = 0; i < count; i++) {
            JacksonUtil.toObject(jsonStr, User.class);
        }
    }

    @Benchmark
    public void gson() {
        for (int i = 0; i < count; i++) {
            GsonUtil.toObject(jsonStr, User.class);
        }
    }
    @Benchmark
    public void fastjson() {
        for (int i = 0; i < count; i++) {
            FastjsonUtil.toObject(jsonStr, User.class);
        }
    }

    @Benchmark
    public void jsonLib(){
        for(int i=0;i<count;i++){
            JsonLibUtil.toObject(jsonStr, User.class);
        }
    }

   @Setup
   public void prepare(){
        jsonStr = "";
   }

   @TearDown
   public void shutdown(){

   }
}
